package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordDateList.persenter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.customview.MyRecyclerView;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordDateList.adpter.HistoricalRecordDateListAdapter;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordDateList.bean.HistoricalRecordListBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordDateList.model.FirstChangeHistoricalRecordDateListModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordDateList.model.ISFirstChangeHistoricalRecordDateListModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeHistoricalRecordDateList.view.ISFirstChangeHistoricalRecordDateListView;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司13
 * Created by 20433 on 2018/3/21
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class FirstChangeHistoricalRecordDateListPersenter {
    private ISFirstChangeHistoricalRecordDateListView mISFirstChangeHistoricalRecordDateListView;
    private HistoricalRecordDateListAdapter mHistoricalRecordDateListAdapter;
    private ISFirstChangeHistoricalRecordDateListModel mISFirstChangeHistoricalRecordDateListModel;
    private int p = 1;

    public FirstChangeHistoricalRecordDateListPersenter(ISFirstChangeHistoricalRecordDateListView iSFirstChangeHistoricalRecordDateListView) {
        this.mISFirstChangeHistoricalRecordDateListView = iSFirstChangeHistoricalRecordDateListView;
        mISFirstChangeHistoricalRecordDateListModel = new FirstChangeHistoricalRecordDateListModel();
    }
    public void initMyRecyclerView(MyRecyclerView activityFirstChangeHistoricalRecordDateListMyrecyclerview, final Context mcontxt){
        LinearLayoutManager layoutManager = new LinearLayoutManager(mcontxt);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.setLayoutManager(layoutManager);
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.setArrowImageView(R.mipmap.iconfont_downgrey);
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.setPullRefreshEnabled(false);  //TODO 禁止下拉刷新完毕
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.getDefaultFootView().setNoMoreHint("就这么多了");
        // TODO 传入数据
        mHistoricalRecordDateListAdapter = new HistoricalRecordDateListAdapter();
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.setAdapter(mHistoricalRecordDateListAdapter);
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.refresh();
        activityFirstChangeHistoricalRecordDateListMyrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                p++;
                loadHistoricalRecordDate(mcontxt,p);
            }
        });
        loadHistoricalRecordDate(mcontxt,p);
    }

    public void loadHistoricalRecordDate(Context mcontxt,int p) {
        if (getUserID(mcontxt) == null) return;
        String url = RBConstants.SERVER_URL + RBConstants.HISTORICAL_RECORD_LIST;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", getUserID(mcontxt).getUser_id())
                .addParams("p", p+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                       // NLogger.e("TAG", "===8877=========NetworkResponseDate=================" + NetworkResponseDate);
                        final HistoricalRecordListBean historicalRecordListBean =   gson.fromJson(NetworkResponseDate, HistoricalRecordListBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(historicalRecordListBean.errorCode == 200){
                                     //适配数据
                                   List<HistoricalRecordListBean.DataBean> mlist =  historicalRecordListBean.data;
                                    if(mlist != null){
                                        mHistoricalRecordDateListAdapter.addItem(mlist);
                                    }

                                }
                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private TakeDateBean getUserID(Context mContext) {
        TakeDateBean bean = mISFirstChangeHistoricalRecordDateListModel.TakeDate(mContext);
        if(bean == null) return null;
        return bean;
    }
}
